﻿#pragma once
#include <iostream>
#include <vector>
#include <assert.h>

using namespace std;
template<class K,class V>
class AVLNode
{
public:
	AVLNode<K, V>* _left;
	AVLNode<K, V>* _right;
	AVLNode<K, V>* _parent;
	int _bf;  // balance factor(平衡因子)
	pair<K, V> _kv;
	AVLNode(const pair<K,V>& kv)
		:_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_bf(0)
		,_kv(kv)
	{}
};

template<class K,class V>
class AVL
{
	typedef AVLNode<K,V> Node;
public:
	AVL()
		:_root(nullptr)
	{}
	AVL(const Node* root)
		:_root(root)
	{}

	void InOrder()
	{
		_InOrder(_root);
	}
	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << ":" << "[" << root->_bf << "]" << endl;
		_InOrder(root->_right);
	}

	//算高度
	int Height()
	{
		_Height(_root);
	}
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	//判断是否是AVL树，看每个节点的高度差和_bf相不相等
	bool IsBalance()
	{
		int i = 0;
		return _IsBalance(_root,i);
	}
	//前序判断，复杂度高，因为重复次数太多
	//bool _IsBalance(Node* root)
	//{
	//	if (root == nullptr)
	//		return true;
	//	int leftHeight = _Height(root->_left);
	//	int rightHeight = _Height(root->_right);
	//	if (abs(leftHeight - rightHeight) > 1)
	//	{
	//		cout << root->_kv.first << root->_bf << "不平衡" << endl;
	//		return false;
	//	}
	//	else if (rightHeight - leftHeight != root->_bf)
	//	{
	//		cout << root->_kv.first << root->_bf << "平衡因子有问题" << endl;
	//		return false;
	//	}
	//	//这个节点判断完，还得判断它的左右孩子
	//	
	//		return _IsBalance(root->_left) && _IsBalance(root->_right);
	//}
	bool _IsBalance(Node* root,int& height)
	{
		if (root == nullptr)
		{
			height = 0;
			return true;
		}
		int leftHeight = 0, rightHeight = 0;
		//左右子树只要有一个不平衡，就是不平衡的
		if (!_IsBalance(root->_left,leftHeight) || !(_IsBalance(root->_right,rightHeight)))
		{
			return false;
		}

		if (abs(leftHeight - rightHeight) > 1)
		{
			cout << root->_kv.first << ':' << root->_bf << "不平衡" << endl;
			return false;
		}
		else if (rightHeight - leftHeight != root->_bf)
		{
			cout << root->_kv.first << ':' << root->_bf << "平衡因子有问题" << endl;
			return false;
		}
		
		//这里算的是我的高度，因为要把我的高度带给上一层
		height = leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;

		return true;

	}
	void RotateL( Node* parent) 
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		//b连到30的右边
		parent->_right = subRL;
		if(subRL)
			subRL->_parent = parent;

		//30连到subR的左边
		subR->_left = parent;

		Node* ppnode = parent->_parent;
		parent->_parent = subR;
		//60变成了根，考虑它的父亲变化
		if (parent == _root)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == ppnode->_left)
			{
				ppnode->_left = subR;
			}
			else if (parent == ppnode->_right)
			{
				ppnode->_right = subR;
			}
			subR->_parent = ppnode;
		}
		parent->_bf = 0;
		subR->_bf = 0;

	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		//b连到60的左边
		parent->_left = subLR;
		if(subLR)
			subLR->_parent = parent;
		//60连到subL的右边
		subL->_right = parent;
		Node* ppnode = parent->_parent;
		parent->_parent = subL;
		//30变成了根，考虑它的父亲变化
		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == ppnode->_left)
			{
				ppnode->_left = subL;
			}
			else if (parent == ppnode->_right)
			{
				ppnode->_right = subL;
			}
			subL->_parent = ppnode;
		}
		parent->_bf = 0;
		subL->_bf = 0;
	}
	void RotateLR( Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		int bf = subLR->_bf;
		RotateL(parent->_left);
		RotateR(parent);
		if (bf == -1)
		{
			subLR->_bf = 0;
			subL->_bf = 0;
			parent->_bf = 1;
		}
		else if (bf == 1)
		{
			subLR->_bf = 0;
			subL->_bf = -1;
			parent->_bf = 0;
		}
		else if (bf == 0)
		{
			subLR->_bf = 0;
			subL->_bf = 0;
			parent->_bf = 0;
		}
		else
		{
			//绝对不会走到这里
			assert(false);
		}
	}

	void RotateRL( Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		int bf = subRL->_bf;
		RotateR(parent->_right);
		RotateL(parent);

		if (bf == 1)
		{
			parent->_bf = -1;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			subR->_bf = 1;
			subRL->_bf = 0;
		}
		else if (bf == 0)
		{
			parent->_bf = 0;
			subR->_bf = 0;
			subRL->_bf = 0;
		}
		else
		{
			assert(false);
		}

	}
	bool insert(const pair<K, V>& kv)
	{
		Node* cur = _root;
		Node* parent = nullptr;
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}

		while (cur)
		{
			if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else if (parent->_kv.first > kv.first)
		{
			parent->_left = cur;

		}
		//这一步是为了保存当前节点的parent，方便后续操作
		cur->_parent = parent;

		//下面就得更新平衡因子了
		while (parent)  //最坏就一直找到根节点了
		{
			if (cur == parent->_left)
			{
				parent->_bf--;
			}
			else
			{
				parent->_bf++;
			}

			if (parent->_bf == 0) //相当于插入后平衡了
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1) //插入后需要更新
			{
				cur = cur->_parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				// 旋转处理
				
				//左旋（右边高）
				if (parent->_bf == 2 && cur->_bf == 1)
				{
					RotateL(parent);
				}
				//右旋（左边高）
				else if (parent->_bf == -2 && cur->_bf == -1)
				{
					RotateR(parent);
				}
				//左右旋
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					RotateLR(parent);
				}
				//右左旋
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
				}
				//旋转处理后，相对根的bf一定会 == 0所以就直接break
				break;
			}
			else
			{
				assert(false);
			}
		}


		return true;
	}
	int Size(Node* root)
	{
		if (root == nullptr)
		{
			return 0;
		}
		return Size(root->_left) + Size(root->_right) + 1;
	}

	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else
				return cur;
		}

		return nullptr;
	}



private:
	Node* _root;
};



void test1()
{
	AVL<int, int> a;
	int arr[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
	for (auto e : arr)
	{
		a.insert(make_pair(e, e));
	}
	a.InOrder();
	cout << a.IsBalance() << endl;

}